package com.aac.framwork.aacframwork.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProvider;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aac.framwork.aacframwork.R;
import com.aac.framwork.aacframwork.entity.User;
import com.aac.framwork.aacframwork.viewmodel.UserProficleViewModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class UserProfileFragment extends Fragment {

    public static final String UID_KEY = "user_id";

    private UserProficleViewModel viewModel;

    //view component
    @BindView(R.id.tv_head)
    TextView tvHead;
    @BindView(R.id.tv_id)
    TextView tvUID;
    @BindView(R.id.tv_name)
    TextView tvName;

    private Unbinder unbinder;


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Long userId = getArguments().getLong(UID_KEY, 0);

        viewModel = ViewModelProviders.of(this).get(UserProficleViewModel.class);
        viewModel.init(userId);

        viewModel.getUser().observe(this, new Observer<User>() {
            @Override
            public void onChanged(@Nullable User user) {

                tvHead.setText(user.getHead());
                tvName.setText(user.getName());
                tvUID.setText(user.getId() + "");
            }
        });

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.user_profile, container, false);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
